#include "widget.h"
#include "ui_widget.h"
#include <QFileDialog>
#include <QSettings>
#include <QDebug>
#include <QStandardPaths>
#include <memory>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->label_image->clear();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::open1()
{
    QString fileName = QFileDialog::getOpenFileName(this, "请选择图片", "D:/", "图片(*.png *.jpg);;");

    if(fileName.isEmpty())
    {
        return;
    }

    //显示路径名字
    ui->lineEdit_path->setText(fileName);

    //显示图片
    ui->label_image->setPixmap(QPixmap(fileName));
}
void Widget::open2()
{
    //用配置文件保存路径
    QString config_path = qApp->applicationDirPath() + "/config/Setting.ini";
    qDebug() << config_path;

    //把每次打开的路径放到 .ini文件中
    QSettings *pIniSet = new QSettings(config_path, QSettings::IniFormat); // (路径，格式)
    QString lastPath = pIniSet->value("LastPath/path").toString(); // 获取里面的值
    if(lastPath.isEmpty())
    {
        //设置为默认路径
        lastPath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
    }

    QString fileName = QFileDialog::getOpenFileName(this, "请选择图片", lastPath, "图片(*.png *.jpg);;");

    if(fileName.isEmpty())
    {
        return;
    }

    //显示路径名字
    ui->lineEdit_path->setText(fileName);

    //显示图片
    ui->label_image->setPixmap(QPixmap(fileName));

    //记录打开的图片路径
    int end = fileName.lastIndexOf("/");
    QString _path = fileName.left(end);
    pIniSet->setValue("LastPath/path", _path);
    delete pIniSet;
    pIniSet = nullptr;
    qDebug() << _path;
}

//智能指针的方式
void Widget::open3()
{
    //用配置文件保存路径
    QString config_path = qApp->applicationDirPath() + "/config/Setting.ini";
    qDebug() << config_path;

    //把每次打开的路径放到 .ini文件中
    std::unique_ptr<QSettings> pIniSet(new QSettings(config_path, QSettings::IniFormat));
    // QSettings *pInitSet = new QSettings(config_path, QSettings::IniFormat); // (路径，格式)
    QString lastPath = pIniSet->value("LastPath/path").toString(); // 获取里面的值
    if(lastPath.isEmpty())
    {
        //设置为默认路径
        lastPath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
    }

    QString fileName = QFileDialog::getOpenFileName(this, "请选择图片", lastPath, "图片(*.png *.jpg);;");

    if(fileName.isEmpty())
    {
        return;
    }

    //显示路径名字
    ui->lineEdit_path->setText(fileName);

    //显示图片
    ui->label_image->setPixmap(QPixmap(fileName));

    //记录打开的图片路径
    int end = fileName.lastIndexOf("/");
    QString _path = fileName.left(end);
    pIniSet->setValue("LastPath/path", _path);
    // delete pInitSet;
    // pInitSet = nullptr;
    qDebug() << _path;
}

void Widget::open4()
{
    //用配置文件保存路径
    QString config_path = qApp->applicationDirPath() + "/config/Setting.ini";
    qDebug() << config_path;

    //把每次打开的路径放到 .ini文件中
    std::unique_ptr<QSettings> pIniSet(new QSettings(config_path, QSettings::IniFormat));
    // QSettings *pInitSet = new QSettings(config_path, QSettings::IniFormat); // (路径，格式)
    QString lastPath = pIniSet->value("LastPath/path").toString(); // 获取里面的值
    if(lastPath.isEmpty())
    {
        //设置为默认路径
        lastPath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
    }

    QString fileName = QFileDialog::getOpenFileName(this, "请选择图片", lastPath, "图片(*.png *.jpg);;");

    if(fileName.isEmpty())
    {
        return;
    }

    //显示路径名字
    ui->lineEdit_path->setText(fileName);

    //显示图片
    //1.设置图片自适应
    QPixmap *pix = new QPixmap(fileName);
    pix->scaled(ui->label_image->size(), Qt::KeepAspectRatio); // 缩放到label大小
    ui->label_image->setScaledContents(true); // 内容也缩放
    //2.显示图片
    ui->label_image->setPixmap(*pix);

    delete pix;
    pix = nullptr;

    //记录打开的图片路径
    int end = fileName.lastIndexOf("/");
    QString _path = fileName.left(end);
    pIniSet->setValue("LastPath/path", _path);
    // delete pInitSet;
    // pInitSet = nullptr;
    qDebug() << _path;
}

void Widget::on_btnOpen_clicked()
{
    //open1();
    //open2();
    //open3();
    open4();
}

